package com.jixiantubu.util;

import java.util.ArrayList;
import com.example.jixiantubu.R;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopMenu extends PopupWindow {
	private ArrayList<String> itemList;
	private Context context;
	private ListView listView;
	private OnPopupWindowClickListener listener;
	private PopAdapter adapter;
	public PopMenu(Context context) {
		super(context);
		this.context = context;
		initViews();
	}
	@SuppressWarnings("deprecation")
	private void initViews(){
		LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.popmenu, null);
		setContentView(view);
		setWidth(250);
		setHeight(android.app.ActionBar.LayoutParams.WRAP_CONTENT);	
		itemList = new ArrayList<String>(3);	
		this.setFocusable(true);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);	
		// 设置 listview
		listView = (ListView) view.findViewById(R.id.popmenu_listview);
		adapter=new PopAdapter(itemList);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				PopMenu.this.dismiss();
				if (listener!=null) {
					listener.onPopupWindowItemClick(arg2);
				}
				
			}
		});
		/*popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		popupWindow = new PopupWindow(view, context.getResources()
				.getDimensionPixelSize(R.dimen.popmenu_width),
				LayoutParams.WRAP_CONTENT);
		// 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景（很神奇的）
		popupWindow.setBackgroundDrawable(new BitmapDrawable());*/
	}
	public void setOnPopupWindowClickListener(
			OnPopupWindowClickListener listener) {
		this.listener = listener;
	}
	public interface OnPopupWindowClickListener {
		/**
		 * 当点击PopupWindow的ListView 的item的时候调用此方法，用回调方法的好处就是降低耦合性
		 * @param position位置
		 */
		void onPopupWindowItemClick(int position);
	}
	// 批量添加菜单项
	public void addItems(String[] items) {
		for (String s : items)
			itemList.add(s);	
	}
	// 单个添加菜单项
	public void addItem(String item) {
		itemList.add(item);
	}

//	// 下拉式 弹出 pop菜单 parent 右下角
//	public void showAsDropDown(View parent) {
//		popupWindow.showAsDropDown(parent,
//				8,
//				// 保证尺寸是根据屏幕像素密度来的
//				context.getResources().getDimensionPixelSize(
//						R.dimen.popmenu_yoff));
//		// 使其聚集
//		popupWindow.setFocusable(true);
//		// 设置允许在外点击消失
//		popupWindow.setOutsideTouchable(true);
//		// 刷新状态
//		popupWindow.update();
//	}
	// 适配器
	private final class PopAdapter extends BaseAdapter {
		private ArrayList<String> itemList;
		public PopAdapter(ArrayList<String> itemList) {
			super();
		this.itemList=itemList;	
		}
		@Override
		public int getCount() {
			return itemList.size();
		}
		@Override
		public Object getItem(int position) {
			return itemList.get(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(
						R.layout.pomenu_item, null);
				holder = new ViewHolder();
				convertView.setTag(holder);
				holder.groupItem = (TextView) convertView
						.findViewById(R.id.pomenu_textview);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.groupItem.setText(itemList.get(position));	
			return convertView;
		}
		private final class ViewHolder {
			TextView groupItem;
		}
	}
}